import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
  },
  {
    path: '/allProject',
    name: 'allProject',
    component: () => import('@/views/AllProject.vue'),
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('@/views/Home.vue'),
    redirect: '/project',
    children: [
      {
        path: '/project',
        component: () => import('@/views/Project.vue'),
        name: 'pro',
      },
      {
        path: '/interface',
        component: () => import('@/views/InterFace.vue'),
        name: 'inter',
      },
      {
        path: '/case',
        component: () => import('@/views/Case.vue'),
        name: 'case',
      },
      {
        path: '/task',
        component: () => import('@/views/Task.vue'),
        name: 'task',
      },
      {
        path: '/runs',
        component: () => import('@/views/Runs.vue'),
        name: 'runs',
      },
      {
        path: '/reports',
        component: () => import('@/views/Reports.vue'),
        name: 'reports',
      },
    ],
  },
]

const router = new VueRouter({
  routes,
})

// 给前端的路由做权限校验
router.beforeEach((to, from, next) => {
  // 获取token
  const isAuthenticated = window.sessionStorage.getItem('token')
  if (to.name !== 'Login' && !isAuthenticated) next({ name: 'Login' })
  next()
})

export default router
